import  torch
from    matplotlib import pyplot as plt
from  torch.nn import  functional as F


def plot_curve(data):
    fig = plt.figure()
    plt.plot(range(len(data)), data, color='blue')
    plt.legend(['value'], loc='upper right')
    plt.xlabel('step')
    plt.ylabel('value')
    plt.show()



def plot_image(img, label, name):

    fig = plt.figure()
    for i in range(6):
        plt.subplot(2, 3, i + 1)
        plt.tight_layout() #tight_layout会自动调整子图参数，使之填充整个图像区域
        plt.imshow(img[i][0]*0.3081+0.1307, cmap='gray', interpolation='none')
        plt.title("{}: {}".format(name, label[i].item()))
        plt.xticks([])
        plt.yticks([])
    plt.show()

def plot_image2(img, label, name):
    fig = plt.figure()
    img = F.interpolate(img, scale_factor=32, mode='nearest')
    img = img.permute(0, 2, 3, 1)

    for i in range(6):
        plt.subplot(2, 3, i + 1)
        plt.tight_layout()  # tight_layout会自动调整子图参数，使之填充整个图像区域
        print(img.shape)
        plt.imshow(img[i])
        plt.title("{}: {}".format(name, label[i]))
        plt.xticks([])
        plt.yticks([])
    plt.show()




def one_hot(label, depth=10):
    out = torch.zeros(label.size(0), depth)
    idx = torch.LongTensor(label).view(-1, 1)
    out.scatter_(dim=1, index=idx, value=1)
    return out